import _forEach from 'lodash.foreach'
import _clonedeep from 'lodash.clonedeep'
import * as service from '@/api/crud.base';
export default {
  data() {
    return {
      targetUrl: '',
      searchForm: {},
      formOptions: {
        saveLoading: false
      },
      selection: [],
      data:[]
    }
  },
  methods: {
    save(row) {
      return service.save(this.targetUrl, row);
    },
    get(id) {
      return service.get(this.targetUrl, id);
    },
    remove(id) {
      return service.del(this.targetUrl, id);
    },
    batchRemove(ids) {
      return service.batchDelete(this.targetUrl, ids);
    },
    list(data) {
      return service.list(this.targetUrl, data);
    },
    add() {
      this.$refs.d2Crud.showDialog({
        mode: 'add'
      })
    },
    selectionHandler(callback,message,multiple){
      let condition = false;
      let param
      if(multiple){
        condition = this.selection && this.selection.length > 0
      }else{
        condition = this.selection && this.selection.length == 1
      }
      if(condition){
        callback(this.selection)
      }else{
        this.$message({
          message: message,
          type: 'warning'
        })
      }
    },
    edit() {
      this.selectionHandler(()=>{
        this.get(this.selection[0].id).then(data => {
          this.handleEdit(data);
        }).catch(err => {
          console.log('err', err)
        })
      },'请选择一条记录修改')
    },
    del() {
      this.selectionHandler(()=>{
        this.$confirm("确认删除？", "确认信息", {
          distinguishCancelAndClose: true,
          confirmButtonText: "删除",
          cancelButtonText: "取消"
        }).then(() => {
          this.remove(this.selection[0].id).then(() => {
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.fetchData();
          }).catch(err => {
            console.log('err', err);
          })
        }).catch(() => {})},'请选择一条记录删除')
    },
    batchDelete() {
      this.selectionHandler(()=>{
        this.$confirm("确认删除？", "确认信息", {
          distinguishCancelAndClose: true,
          confirmButtonText: "删除",
          cancelButtonText: "取消"
        }).then(() => {
          this.batchRemove(this.selection.map(s => s.id)).then(() => {
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.fetchData();
          }).catch(err => {
            console.log('err', err);
          });
        }).catch(() => {})},'请选择一条记录删除',true)
    },
    handleRowAdd(row, done) {
      this.formOptions.saveLoading = true
      this.save(row).then(data => {
        this.$message({
          message: '保存成功',
          type: 'success'
        });
        this.fetchData();
        done()
      });
      this.formOptions.saveLoading = false
    },
    handleRowEdit({
      index,
      row
    }, done) {
      this.formOptions.saveLoading = true
      this.save(row).then(data => {
        this.$message({
          message: '保存成功',
          type: 'success'
        });
        this.fetchData();
        done()
      });
      this.formOptions.saveLoading = false
    },
    fetchData(data) {
      this.loading = true
      var param = {};
      Object.assign(param,data,this.searchForm)
      let page = this.$refs.pagination;
      if (page) {
        Object.assign(param,{currentPage: page.currentPage,pageSize: page.pageSize});
      }
      this.list(param).then(res => {
        if (page) {
          page.total = res.total
          this.data = res.data
        }else{
          this.data=res;
        }
        this.loading = false
      }).catch(err => {
        console.log('err', err)
        this.loading = false
      })
    },
    handleDialogCancel(done) {
      done();
    },
    handleSearchFormSubmit() {
      this.fetchData();
    },
    handleSearchFormReset() {
      this.$refs.searchForm.resetFields();
    },
    handleSelectionChange(val) {
      this.selection = val;
    },
    handleEdit(row, templage = null) {
      let d2crud = this.$refs.d2Crud;
      d2crud.formMode = 'edit'
      d2crud.editDataStorage = _clonedeep(row)
      d2crud.isDialogShow = true
      d2crud.$emit('dialog-open', {
        mode: 'edit',
        row
      })
      if (templage) {
        d2crud.formData = _clonedeep(templage)
        d2crud.editTemplateStorage = _clonedeep(templage)
      } else {
        d2crud.formData = d2crud.editTemplate ? _clonedeep(d2crud.editTemplate) : {}
        d2crud.editTemplateStorage = d2crud.editTemplate ? _clonedeep(d2crud.editTemplate) : {}
      }
      _forEach(d2crud.formData, (value, key) => {
        d2crud.formData[key] = row.hasOwnProperty(key) ? row[key] : ''
      })
    }
  },
  mounted() {
    this.fetchData();
  }
}
